﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Constructor
{
    public partial class EditorSlot : Form
    {
        private Frame _frame;
        public EditorSlot()
        {
            InitializeComponent();
        }
        public EditorSlot(Frame frame)
        {
            InitializeComponent();
            _frame = frame;
            External.frames.Remove(_frame);
            Frame temp = _frame.Clone() as Frame;
            External.frames.Add(temp);
            slotConstructor1.Init(temp);
            Text = _frame.Name;
        }
        public EditorSlot(Frame frame, Slot s)
        {
            InitializeComponent();
            _frame = frame;
            slotConstructor1.Init(_frame.Clone() as Frame,s);
            Text = _frame.Name;
        }
        bool save = false;
        private void btn_ok_Click(object sender, EventArgs e)
        {            
            Frame newFrame = slotConstructor1.Frame;
            newFrame.Node.Tag = newFrame;         
            External.constructor.propertiesPanel.Init(newFrame);
            foreach (Frame f in External.frames)
            {
                foreach (Slot slot in f.Slots)
                    if ((slot.Type == CommonLib.SlotType.SubFrame || slot.Type == CommonLib.SlotType.AKO) && slot.Subframe != null && slot.Subframe.Name == newFrame.Name)
                        slot.Subframe = newFrame;
            }
            newFrame.RepaintNode(true);
            newFrame.Node.ResizeToFitText(false);
            save = true;
            Close();
        }

        private void btn_cancel_Click(object sender, EventArgs e)
        {
            Close();
            _frame.RepaintNode(true);
        }

        private void slotConstructor1_Load(object sender, EventArgs e)
        {

        }

        private void EditorSlot_FormClosed(object sender, FormClosedEventArgs e)
        {
            if (!save)
            {
                External.frames.Remove(slotConstructor1.Frame);
                External.frames.Add(_frame);
            }
        }        

    }
}
